/*
 * Copyright ©2015-2021 Jaemon. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.jaemon.spring.adorner;

import io.gitee.jaemon.spring.adorner.core.*;
import io.gitee.jaemon.spring.adorner.exception.AdornerException;
import io.gitee.jaemon.spring.adorner.handler.ParamDefinitionHandler;
import io.gitee.jaemon.spring.adorner.support.AdornerBanner;
import io.gitee.jaemon.spring.adorner.type.ParamPair;

import java.text.MessageFormat;

import static io.gitee.jaemon.spring.adorner.AdornerConstants.ADORNER_SOURCE;
import static io.gitee.jaemon.spring.adorner.util.LoggerHelper.error;

/**
 * Adorner启动类
 *
 * @author Jaemon
 * @since 1.0
 */
public class SpringAdornerStarter {

    /**
     * 采用String文档生成器生成API接口文档
     *
     * @param filePath
     *      文档存储路径
     * @param packageName
     *      包名 <code>io.gitee.jaemon.spring.adorner.contorller</code>
     * @see StringAdornerExecutor
     */
    public static void generate(String filePath, String packageName) {
        generate(filePath, new String[]{packageName});
    }

    /**
     * 采用String文档生成器生成API接口文档
     *
     * @param filePath
     *      文档存储路径
     * @param packageName
     *      包名 <code>io.gitee.jaemon.spring.adorner.contorller</code>
     * @param noteScanner
     *      注释扫描器-用于自定义注解进行注释时
     * @see StringAdornerExecutor
     */
    public static void generate(
            String filePath, String packageName, NoteScanner noteScanner
    ) {
        generate(filePath, new String[]{packageName}, noteScanner);
    }

    /**
     * 采用String文档生成器生成API接口文档
     *
     * @param filePath
     *      文档存储路径
     * @param packages
     *      包名数组
     * @see StringAdornerExecutor
     */
    public static void generate(String filePath, String[] packages) {
        try {
            generate(
                    AdornerApplicationContext.builder().build(filePath, packages)
            );
        } catch (AdornerException ex) {
            error("exception: %s", ex.getMessage());
        }
    }

    /**
     * 采用String文档生成器生成API接口文档
     *
     * @param filePath
     *      文档存储路径
     * @param packages
     *      包名数组
     * @param noteScanner
     *      注释扫描器-用于自定义注解进行注释时
     * @see StringAdornerExecutor
     */
    public static void generate(
            String filePath, String[] packages, NoteScanner noteScanner
    ) {
        try {
            generate(
                    AdornerApplicationContext.builder().build(filePath, packages),
                    noteScanner
            );
        } catch (AdornerException ex) {
            error("exception: %s", ex.getMessage());
        }
    }

    /**
     * 采用String文档生成器生成API接口文档
     *
     * @param context
     *      Adorner上下文
     * @see StringAdornerExecutor
     */
    public static void generate(AdornerApplicationContext context) {
        try {
            Executor executor = new StringAdornerExecutor(context);
            executor.execute();
        } catch (AdornerException ex) {
            error("exception: %s", ex.getMessage());
        }
    }

    /**
     * 采用String文档生成器生成API接口文档
     *
     * @param context
     *      Adorner上下文
     * @param docGenerator
     *      {@link ParamPair}类型文档生成器
     * @see StringAdornerExecutor
     */
    public static void generate(
            AdornerApplicationContext context,
            DocGenerator<ParamPair> docGenerator
    ) {
        try {
            Executor executor = new StringAdornerExecutor(context, docGenerator);
            executor.execute();
        } catch (AdornerException ex) {
            error("exception: %s", ex.getMessage());
        }
    }

    /**
     * 采用String文档生成器生成API接口文档
     *
     * @param context
     *      Adorner上下文
     * @param noteScanner
     *      注释扫描器-用于自定义注解进行注释时
     * @see StringAdornerExecutor
     */
    public static void generate(AdornerApplicationContext context, NoteScanner noteScanner) {
        try {
            Executor executor = new StringAdornerExecutor(context, noteScanner);
            executor.execute();
        } catch (AdornerException ex) {
            error("exception: %s", ex.getMessage());
        }
    }

    /**
     * 采用String文档生成器生成API接口文档
     *
     * @param context
     *      Adorner上下文
     * @param paramHandler
     *      {@link ParamPair}类型参数处理器
     * @param noteScanner
     *      注释扫描器-用于自定义注解进行注释时
     * @see StringAdornerExecutor
     */
    public static void generate(
            AdornerApplicationContext context,
            ParamHandler<ParamPair> paramHandler,
            NoteScanner noteScanner
    ) {
        try {
            Executor executor = new StringAdornerExecutor(context, paramHandler, noteScanner);
            executor.execute();
        } catch (AdornerException ex) {
            error("exception: %s", ex.getMessage());
        }
    }

    /**
     * 采用String文档生成器生成API接口文档
     *
     * @param context
     *      Adorner上下文
     * @param docGenerator
     *      {@link ParamPair}类型文档生成器
     * @param noteScanner
     *      注释扫描器-用于自定义注解进行注释时
     * @see StringAdornerExecutor
     */
    public static void generate(
            AdornerApplicationContext context,
            DocGenerator<ParamPair> docGenerator,
            NoteScanner noteScanner
    ) {
        try {
            Executor executor = new StringAdornerExecutor(context, docGenerator, noteScanner);
            executor.execute();
        } catch (AdornerException ex) {
            error("exception: %s", ex.getMessage());
        }
    }

    /**
     * 采用String文档生成器生成API接口文档
     *
     * @param context
     *      Adorner上下文
     * @param paramHandler
     *      {@link ParamPair}类型参数处理器
     * @param docGenerator
     *      {@link ParamPair}类型文档生成器
     * @param noteScanner
     *      注释扫描器-用于自定义注解进行注释时
     * @see StringAdornerExecutor
     */
    public static void generate(
            AdornerApplicationContext context,
            ParamHandler<ParamPair> paramHandler,
            DocGenerator<ParamPair> docGenerator,
            NoteScanner noteScanner
    ) {
        try {
            Executor executor = new StringAdornerExecutor(context, paramHandler, docGenerator, noteScanner);
            executor.execute();
        } catch (AdornerException ex) {
            error("exception: %s", ex.getMessage());
        }
    }

    /**
     * 生成API接口文档
     *
     * @param executor
     *      自定义执行器
     * @param <T>
     *      T
     * @see ParamDefinitionHandler
     */
    public static <T> void generate(
            AbstractExecutor<T> executor
    ) {
        try {
            executor.execute();
        } catch (Exception ex) {
            error("exception: %s", ex.getMessage());
        }
    }

    public static void main(String[] args) {
        System.out.println(new AdornerBanner().printBanner());

        System.out.println(
                MessageFormat.format(
                        "Getting Started：{0}/wikis/Home?#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B",
                        ADORNER_SOURCE
                )
        );
    }

}